package com.example.flink.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StreamWordCount
 * Package: com.example.flink.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-20
 * Time: 19:45
 */


//流式计算
public class StreamWordCount {

    public static void main(String[] args) throws Exception {

        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置并行度
        env.setParallelism(1);

        //直接创建FileSource 从文件中读取数据 要使用到外部依赖
        //FileSource<String> build =
                //FileSource.forRecordStreamFormat(new TextLineInputFormat(), new Path("")).build();

        //流式计算 累加效果 来一条 计算一条
        DataStreamSource<String> data = env.readTextFile("D:\\data\\hadoop\\Flink_review\\word.txt");

        //从文件中读取数据 要使用到外部依赖
        SingleOutputStreamOperator<Tuple2<String, Integer>> dataTup = data.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {

            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {

                //一行一行处理
                //对读进来的数据 切割
                String[] s = value.split(" ");

                //循环遍历
                for (String s1 : s) {
                    out.collect(new Tuple2(s1, 1));
                }
            }
        });

        //走到这里 (word,1) 对单词进行聚合
        final KeyedStream<Tuple2<String, Integer>, String> dataKey = dataTup.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
            @Override
            public String getKey(Tuple2<String, Integer> value) throws Exception {
                return value.f0;
            }
        });

        //sum 简单聚合算子 只有keyBy之后 才能使聚合算子 对于元组类型 参数传递下标 对于Bean对象 参数传递字段
        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = dataKey.sum(1);

        sum.print();
        env.execute();

    }

}
